package com.tepth.vehiclemaintenance.adapter;

import android.content.Context;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.bean.MaintainListWork;
import com.tepth.vehiclemaintenance.mvp.work.content.RemarkDialog;
import com.tepth.vehiclemaintenance.tools.Utils;

import java.util.List;


/**
 * Created by LHQ on 2016/12/19.
 */

public class GuojianAdapter extends BaseAdapter {

    private List<MaintainListWork> mDatas;
    private Context mContext;
    private LayoutInflater layoutInflater;
    private VectorDrawableCompat mDrawableCompatNormal;
    private VectorDrawableCompat mDrawableCompatCancel;
    private VectorDrawableCompat mDrawableCompatComplete;

    public GuojianAdapter(Context context, List<MaintainListWork> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;
        layoutInflater = LayoutInflater.from(this.mContext);
        mDrawableCompatNormal = VectorDrawableCompat.create(context.getResources(),
                R.drawable.ic_check_circle_gray_24dp, context.getTheme());
        mDrawableCompatCancel = VectorDrawableCompat.create(context.getResources(),
                R.drawable.ic_check_circle_red_24dp, context.getTheme());
        mDrawableCompatComplete = VectorDrawableCompat.create(context.getResources(),
                R.drawable.ic_check_circle_green_24dp, context.getTheme());
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.maintain_details_guojian_item, parent, false);
            viewHolder.tv_guojian_details = (TextView) convertView.findViewById(R.id.tv_guojian_details);
            viewHolder.tv_guojian_name = (TextView) convertView.findViewById(R.id.tv_guojian_name);
            viewHolder.guojian_checkbox = (ImageView) convertView.findViewById(R.id.guojian_checkbox);
            viewHolder.iv_remark = (ImageView) convertView.findViewById(R.id.iv_remark);
            viewHolder.tv_remark = (TextView) convertView.findViewById(R.id.tv_remark);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final MaintainListWork obj = mDatas.get(position);
        //修改添加备注 by zhangyu 2017.4.13 15:44
        final String remark = obj.getCompletedRemark();
        if (remark == null || remark.isEmpty() || "null".equals(remark)) {
            viewHolder.tv_remark.setVisibility(View.GONE);
        } else {
            viewHolder.tv_remark.setVisibility(View.VISIBLE);
            viewHolder.tv_remark.setText(remark);
        }

        viewHolder.iv_remark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new RemarkDialog(mContext, remark, new RemarkDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm(String remark) {
                        obj.setCompletedRemark(remark);
                        notifyDataSetChanged();
                    }
                }).show();
            }
        });

        viewHolder.tv_guojian_details.setText(obj.getContentTypeName());
        if (obj.getConfirmPersonName() == null || obj.getConfirmPersonName().isEmpty() ||
                "null".equals(obj.getConfirmPersonName())) {
            viewHolder.tv_guojian_name.setVisibility(View.GONE);
            if (obj.isGuojian()) {
                viewHolder.guojian_checkbox.setImageDrawable(mDrawableCompatCancel);
                viewHolder.iv_remark.setVisibility(View.GONE);
            } else {
                viewHolder.guojian_checkbox.setImageDrawable(mDrawableCompatNormal);
                viewHolder.iv_remark.setVisibility(View.VISIBLE);
            }
        } else {
            if (viewHolder.tv_guojian_details.getCurrentTextColor() == Utils.getColor(mContext, R.color.colorPrimary)) {
                viewHolder.tv_guojian_name.setVisibility(View.GONE);
                viewHolder.guojian_checkbox.setImageDrawable(mDrawableCompatNormal);
                viewHolder.iv_remark.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tv_guojian_name.setVisibility(View.VISIBLE);
                viewHolder.tv_guojian_name.setText(obj.getConfirmPersonName());
                viewHolder.guojian_checkbox.setImageDrawable(mDrawableCompatComplete);
                viewHolder.iv_remark.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView tv_guojian_details, tv_guojian_name, tv_remark;
        ImageView guojian_checkbox, iv_remark;
    }
}
